package com.xyz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xyz.dao.mapper.PatrolPlanDaoMapper;
import com.xyz.dao.mapper.TemplatePlanDaoMapper;
import com.xyz.dao.mapper.UpkeepDaoMapper;
import com.xyz.dao.pojo.PatrolPlanCalendarVo;
import com.xyz.dao.pojo.PlanCalendarVo;
import com.xyz.dao.pojo.WeekMothVo;
import com.xyz.dto.PatrolPlanDto;
import com.xyz.dto.PlanInfoDto;
import com.xyz.dto.UpkeepCalendarDto;
import com.xyz.entity.mapper.*;
import com.xyz.entity.pojo.*;
import com.xyz.entity.vo.UpkeepDbTaskExportVo;
import com.xyz.service.CommonService;
import com.xyz.service.PlanInfoService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.*;
import java.util.stream.Collectors;


@Service
@Transactional(rollbackFor = Exception.class)
public class PlanInfoServiceImpl implements PlanInfoService {

    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Autowired
    private EquipmentTreeMapper equipmentTreeMapper;

    @Autowired
    private TemplatePlanMapper templatePlanMapper;

    @Autowired
    private EquipmentPlanTemplateMapper EquipmentPlanTemplateMapper;

    @Autowired
    private TemplatePlanDaoMapper templatePlanDao;

    @Autowired
    private PatrolPlanDaoMapper patrolPlanDaoMapper;

    @Autowired
    private UpkeepDbTaskMapper upkeepDbTaskMapper;

    @Autowired
    private UpkeepTemplateTaskMapper upkeepTemplateTaskMapper;

    @Autowired
    private UpkeepDaoMapper upkeepDaoMapper;

    /*
     * 查询计划列表
     * */
    @Override
    public DataResult getPlanAll(LoginInfo loginInfo, PlanInfoDto planInfoDto) throws Exception {
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(planInfoDto.getEmGroup()));
        List<Long> treeIdList = new ArrayList<>();
        treeIdList.add(et.getId());
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        etExample.createCriteria().andBaseIdEqualTo(et.getId());
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);
        for (EquipmentTree ett : equipmentTrees) {
            treeIdList.add(ett.getId());
        }

        int yearInt = Integer.parseInt(planInfoDto.getYear());
        EquipmentInfoExample eiExample = new EquipmentInfoExample();
        EquipmentInfoExample.Criteria eiCriteria = eiExample.createCriteria();
        eiCriteria.andEmTreeIn(treeIdList);
        eiExample.setOrderByClause("create_time");
        Page<EquipmentInfo> pageObject = PageHelper.startPage(Integer.parseInt(planInfoDto.getPageNum()), Integer.parseInt(planInfoDto.getPageSize()));
        List<EquipmentInfo> eiList = equipmentInfoMapper.selectByExample(eiExample);
        List<Long> emIdList = eiList.stream().map(EquipmentInfo::getId).collect(Collectors.toList());
        List<PlanCalendarVo> planCalendarVos = new ArrayList<>();
        if (CollUtil.isNotEmpty(emIdList)) {
            planCalendarVos = templatePlanDao.selectPlanInfoByYear(emIdList, yearInt, Integer.parseInt(planInfoDto.getPlanType()));
        }
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> planRowList = getPlanRowList(yearInt, Integer.parseInt(planInfoDto.getType()), planCalendarVos);
        Map<Long, List<Map<String, Object>>> templateGroupMap = (Map<Long, List<Map<String, Object>>>) planRowList.get("TemplateGroupMap");
        for (EquipmentInfo equipmentInfo : eiList) {
            Map<String, Object> map = new HashMap<>();
            map.put("emName", equipmentInfo.getEmName());
            map.put("emId", equipmentInfo.getId());
            map.put("list", templateGroupMap.get(equipmentInfo.getId()));
            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("headDateList", planRowList.get("headData"));
        map.put("total", pageObject.getTotal());
        map.put("rows", list);
        return DataResult.build9100(map);
    }

    /*
     * 根据日期查询计划
     * */
    @Override
    public DataResult getPlanTemplateByDate(LoginInfo loginInfo, PlanInfoDto planInfoDto) throws Exception {
        if (StrUtil.isBlank(planInfoDto.getPageNum()) || StrUtil.isBlank(planInfoDto.getPageSize())) {
            return DataResult.build9200("pageNum或pageNum数据缺失");
        }
        int yearInt = Integer.parseInt(planInfoDto.getYear());
        int column = Integer.parseInt(planInfoDto.getColumn());
        Page<Map<String, Object>> pageObject = PageHelper.startPage(Integer.parseInt(planInfoDto.getPageNum()), Integer.parseInt(planInfoDto.getPageSize()));
        Long templateId = null;
        if (StrUtil.isNotBlank(planInfoDto.getTemplateId())) {
            templateId = Long.parseLong(planInfoDto.getTemplateId());
        }
        Integer businessType = null;
        if (StrUtil.isNotBlank(planInfoDto.getBusinessType())) {
            businessType = Integer.parseInt(planInfoDto.getBusinessType());
        }
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(planInfoDto.getEmGroup()));
        List<Long> treeIdList = new ArrayList<>();
        treeIdList.add(et.getId());
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        etExample.createCriteria().andBaseIdEqualTo(et.getId());
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);
        for (EquipmentTree ett : equipmentTrees) {
            treeIdList.add(ett.getId());
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        //type=1 日 type=2 周 type=3 年
        if (planInfoDto.getType().equals("1")) {
            int month = Integer.parseInt(planInfoDto.getMonth());
            cal.set(yearInt, month - 1, column);
            mapList = templatePlanDao.selectPlanTemplateByDay(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), planInfoDto.getEmName(), templateId, businessType, cal.getTime());
        } else if (planInfoDto.getType().equals("2")) {
            for (int i = 1; i < 8; i++) {
                cal.set(yearInt, 0, i);
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    break;
                }
            }
            int setWeekNum = column - 1;
            Date startTime;
            Date endTime;
            if (setWeekNum == 0) {
                Calendar calWeek = Calendar.getInstance();
                calWeek.set(yearInt, 0, 1);
                startTime = calWeek.getTime();
                endTime = cal.getTime();
            } else {
                endTime = DateUtil.offsetWeek(cal.getTime(), setWeekNum);
                startTime = DateUtil.offsetDay(endTime, -6);
            }
            mapList = templatePlanDao.selectPlanTemplateByDate(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), planInfoDto.getEmName(), templateId, businessType, startTime, endTime);
        } else if (planInfoDto.getType().equals("3")) {
            cal.set(yearInt, column - 1, 1);
            Date startTime = cal.getTime();
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(yearInt, column - 1, Maximum);
            Date endTime = cal.getTime();
            mapList = templatePlanDao.selectPlanTemplateByDate(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), planInfoDto.getEmName(), templateId, businessType, startTime, endTime);
        }

        return DataResult.build9100(new GridData(pageObject.getTotal(), mapList));
    }

    @Override
    public DataResult selectTemplateGroup(LoginInfo loginInfo, PlanInfoDto planInfoDto) throws Exception{
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(planInfoDto.getEmGroup()));
        List<Long> treeIdList = new ArrayList<>();
        treeIdList.add(et.getId());
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        etExample.createCriteria().andBaseIdEqualTo(et.getId());
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);
        for (EquipmentTree ett : equipmentTrees) {
            treeIdList.add(ett.getId());
        }
        int yearInt = Integer.parseInt(planInfoDto.getYear());
        int column = Integer.parseInt(planInfoDto.getColumn());
        List<Map<String, Object>> mapList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        //type=1 日 type=2 周 type=3 年
        if (planInfoDto.getType().equals("1")) {
            int month = Integer.parseInt(planInfoDto.getMonth());
            cal.set(yearInt, month - 1, column);
            mapList = templatePlanDao.selectPlanTemplateByDayGroup(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), cal.getTime());
        } else if (planInfoDto.getType().equals("2")) {
            for (int i = 1; i < 8; i++) {
                cal.set(yearInt, 0, i);
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    break;
                }
            }
            int setWeekNum = column - 1;
            Date startTime;
            Date endTime;
            if (setWeekNum == 0) {
                Calendar calWeek = Calendar.getInstance();
                calWeek.set(yearInt, 0, 1);
                startTime = calWeek.getTime();
                endTime = cal.getTime();
            } else {
                endTime = DateUtil.offsetWeek(cal.getTime(), setWeekNum);
                startTime = DateUtil.offsetDay(endTime, -6);
            }
            mapList = templatePlanDao.selectPlanTemplateByDateGroup(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), startTime, endTime);
        } else if (planInfoDto.getType().equals("3")) {
            cal.set(yearInt, column - 1, 1);
            Date startTime = cal.getTime();
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(yearInt, column - 1, Maximum);
            Date endTime = cal.getTime();
            mapList = templatePlanDao.selectPlanTemplateByDateGroup(treeIdList, Integer.parseInt(planInfoDto.getPlanType()), startTime, endTime);
        }
        return DataResult.build9100(mapList);
    }

    @Override
    public DataResult getPatrolPlanAll(LoginInfo loginInfo, PatrolPlanDto patrolPlanDto) throws Exception{
        int yearInt = Integer.parseInt(patrolPlanDto.getYear());
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(patrolPlanDto.getTreeId()));
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        //类型：0区域，1系统
        if(et.getType() == 1){
            etExample.createCriteria().andBaseIdEqualTo(et.getId());
        }else if(et.getType() == 0){
            EquipmentTreeExample etExample2 = new EquipmentTreeExample();
            etExample2.createCriteria().andBaseIdEqualTo(et.getId());
            List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample2);
            List<Long> baseIdList = new ArrayList<>();
            for (EquipmentTree ett : equipmentTrees) {
                baseIdList.add(ett.getId());
            }
            etExample.createCriteria().andBaseIdIn(baseIdList);
        }else {
            return DataResult.build9250("获取的不是区域或者系统");
        }
        etExample.setOrderByClause("sort");
        //返回值
        List<Map<String, Object>> mapList = new ArrayList<>();
        Page<EquipmentTree> pageObject = PageHelper.startPage(Integer.parseInt(patrolPlanDto.getPageNum()), Integer.parseInt(patrolPlanDto.getPageSize()));
        //查询级结构树
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);

        List<Long> treeIdList = equipmentTrees.stream().map(EquipmentTree::getId).collect(Collectors.toList());
        List<PatrolPlanCalendarVo> planCalendarVos = new ArrayList<>();
        if (CollUtil.isNotEmpty(treeIdList)) {
            planCalendarVos = patrolPlanDaoMapper.selectPlanInfoByYear(treeIdList, yearInt);
        }
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> planRowList = getPatrolPlanRowList(yearInt, Integer.parseInt(patrolPlanDto.getType()), planCalendarVos);
        Map<Long, List<Map<String, Object>>> templateGroupMap = (Map<Long, List<Map<String, Object>>>) planRowList.get("TemplateGroupMap");
        for (EquipmentTree equipmentTree : equipmentTrees) {
            Map<String, Object> map = new HashMap<>();
            map.put("treeName", equipmentTree.getName());
            map.put("treeId", equipmentTree.getId());
            map.put("list", templateGroupMap.get(equipmentTree.getId()));
            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("headDateList", planRowList.get("headData"));
        map.put("total", pageObject.getTotal());
        map.put("rows", list);
        return DataResult.build9100(map);
    }

    @Override
    public DataResult getPatrolPlanTemplateByDate(LoginInfo loginInfo, PatrolPlanDto patrolPlanDto) throws Exception {
        if (StrUtil.isBlank(patrolPlanDto.getPageNum()) || StrUtil.isBlank(patrolPlanDto.getPageSize())) {
            return DataResult.build9200("pageNum或pageNum数据缺失");
        }
        int yearInt = Integer.parseInt(patrolPlanDto.getYear());
        int column = Integer.parseInt(patrolPlanDto.getColumn());
        Page<Map<String, Object>> pageObject = PageHelper.startPage(Integer.parseInt(patrolPlanDto.getPageNum()), Integer.parseInt(patrolPlanDto.getPageSize()));
        Long templateId = null;
        if (StrUtil.isNotBlank(patrolPlanDto.getTemplateId())) {
            templateId = Long.parseLong(patrolPlanDto.getTemplateId());
        }
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(patrolPlanDto.getTreeId()));
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        //类型：0区域，1系统
        if(et.getType() == 1){
            etExample.createCriteria().andBaseIdEqualTo(et.getId());
        }else if(et.getType() == 0){
            EquipmentTreeExample etExample2 = new EquipmentTreeExample();
            etExample2.createCriteria().andBaseIdEqualTo(et.getId());
            List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample2);
            List<Long> baseIdList = new ArrayList<>();
            for (EquipmentTree ett : equipmentTrees) {
                baseIdList.add(ett.getId());
            }
            etExample.createCriteria().andBaseIdIn(baseIdList);
        }else {
            return DataResult.build9250("获取的不是区域或者系统");
        }
        etExample.setOrderByClause("sort");
        //返回值
        //查询级结构树
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);
        List<Long> treeIdList = new ArrayList<>();
        for (EquipmentTree ett : equipmentTrees) {
            treeIdList.add(ett.getId());
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        //type=1 日 type=2 周 type=3 年
        if (patrolPlanDto.getType().equals("1")) {
            int month = Integer.parseInt(patrolPlanDto.getMonth());
            cal.set(yearInt, month - 1, column);
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDay(treeIdList, patrolPlanDto.getTreeName(), templateId, cal.getTime());
        } else if (patrolPlanDto.getType().equals("2")) {
            for (int i = 1; i < 8; i++) {
                cal.set(yearInt, 0, i);
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    break;
                }
            }
            int setWeekNum = column - 1;
            Date startTime;
            Date endTime;
            if (setWeekNum == 0) {
                Calendar calWeek = Calendar.getInstance();
                calWeek.set(yearInt, 0, 1);
                startTime = calWeek.getTime();
                endTime = cal.getTime();
            } else {
                endTime = DateUtil.offsetWeek(cal.getTime(), setWeekNum);
                startTime = DateUtil.offsetDay(endTime, -6);
            }
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDate(treeIdList, patrolPlanDto.getTreeName(), templateId, startTime,endTime);
        } else if (patrolPlanDto.getType().equals("3")) {
            cal.set(yearInt, column - 1, 1);
            Date startTime = cal.getTime();
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(yearInt, column - 1, Maximum);
            Date endTime = cal.getTime();
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDate(treeIdList, patrolPlanDto.getTreeName(), templateId, startTime,endTime);
        }

        return DataResult.build9100(new GridData(pageObject.getTotal(), mapList));
    }

    @Override
    public DataResult selectPatrolTemplateGroup(LoginInfo loginInfo, PatrolPlanDto patrolPlanDto) {
        EquipmentTree et = equipmentTreeMapper.selectByPrimaryKey(Long.parseLong(patrolPlanDto.getTreeId()));
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        //类型：0区域，1系统
        if(et.getType() == 1){
            etExample.createCriteria().andBaseIdEqualTo(et.getId());
        }else if(et.getType() == 0){
            EquipmentTreeExample etExample2 = new EquipmentTreeExample();
            etExample2.createCriteria().andBaseIdEqualTo(et.getId());
            List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample2);
            List<Long> baseIdList = new ArrayList<>();
            for (EquipmentTree ett : equipmentTrees) {
                baseIdList.add(ett.getId());
            }
            etExample.createCriteria().andBaseIdIn(baseIdList);
        }else {
            return DataResult.build9250("获取的不是区域或者系统");
        }
        etExample.setOrderByClause("sort");
        //返回值
        //查询级结构树
        List<EquipmentTree> equipmentTrees = equipmentTreeMapper.selectByExample(etExample);
        List<Long> treeIdList = new ArrayList<>();
        for (EquipmentTree ett : equipmentTrees) {
            treeIdList.add(ett.getId());
        }
        int yearInt = Integer.parseInt(patrolPlanDto.getYear());
        int column = Integer.parseInt(patrolPlanDto.getColumn());
        List<Map<String, Object>> mapList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        //type=1 日 type=2 周 type=3 年
        if (patrolPlanDto.getType().equals("1")) {
            int month = Integer.parseInt(patrolPlanDto.getMonth());
            cal.set(yearInt, month - 1, column);
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDayGroup(treeIdList, cal.getTime());
        } else if (patrolPlanDto.getType().equals("2")) {
            for (int i = 1; i < 8; i++) {
                cal.set(yearInt, 0, i);
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    break;
                }
            }
            int setWeekNum = column - 1;
            Date startTime;
            Date endTime;
            if (setWeekNum == 0) {
                Calendar calWeek = Calendar.getInstance();
                calWeek.set(yearInt, 0, 1);
                startTime = calWeek.getTime();
                endTime = cal.getTime();
            } else {
                endTime = DateUtil.offsetWeek(cal.getTime(), setWeekNum);
                startTime = DateUtil.offsetDay(endTime, -6);
            }
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDateGroup(treeIdList, startTime, endTime);
        } else if (patrolPlanDto.getType().equals("3")) {
            cal.set(yearInt, column - 1, 1);
            Date startTime = cal.getTime();
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(yearInt, column - 1, Maximum);
            Date endTime = cal.getTime();
            mapList = patrolPlanDaoMapper.selectPlanTemplateByDateGroup(treeIdList, startTime, endTime);
        }
        return DataResult.build9100(mapList);
    }

    /*
    * 根据设备ID查询工作项
    * */
    @Override
    public DataResult selectUpkeepDbTask(LoginInfo loginInfo, String emId, String taskName) throws Exception {
        UpkeepDbTaskExample example = new UpkeepDbTaskExample();
        UpkeepDbTaskExample.Criteria criteria = example.createCriteria();
        criteria.andEmIdEqualTo(Long.parseLong(emId));
        if(StrUtil.isNotBlank(taskName)){
            criteria.andTaskNameLike("%" + taskName + "%");
        }
        List<UpkeepDbTask> upkeepDbTasks = upkeepDbTaskMapper.selectByExample(example);
        List<Map<String,Object>> list = new ArrayList<>();
        for(UpkeepDbTask up:upkeepDbTasks){
            Map<String,Object> map = new HashMap<>();
            map.put("id",up.getId());
            map.put("taskName",up.getTaskName());
            map.put("taskDesc",up.getTaskDesc());
            map.put("remark",up.getRemark());
            map.put("precautions",up.getPrecautions());
            list.add(map);
        }
        return DataResult.build9100(list);
    }

    @Override
    public DataResult selectBasicUpkeepDbTask(LoginInfo loginInfo, int pageNum, int pageSize, String treeId, String emId, String taskName) throws Exception {
        UpkeepDbTaskExample example = new UpkeepDbTaskExample();
        UpkeepDbTaskExample.Criteria criteria = example.createCriteria();

        if(StrUtil.isNotBlank(treeId)){
            criteria.andTreeIdEqualTo(Long.parseLong(treeId));
        }

        if(StrUtil.isNotBlank(emId)){
            criteria.andEmIdEqualTo(Long.parseLong(emId));
        }

        if(StrUtil.isNotBlank(taskName)){
            criteria.andTaskNameLike("%" + taskName + "%");
        }
        Page<EquipmentInfo> page = PageHelper.startPage(pageNum, pageSize);
        List<UpkeepDbTask> upkeepDbTasks = upkeepDbTaskMapper.selectByExample(example);
        List<Map<String,Object>> list = new ArrayList<>();
        for(UpkeepDbTask up:upkeepDbTasks){
            Map<String,Object> map = new HashMap<>();
            map.put("id",up.getId());
            map.put("emId",up.getEmId());
            map.put("treeId",up.getTreeId());
            map.put("taskName",up.getTaskName());
            map.put("taskDesc",up.getTaskDesc());
            map.put("remark",up.getRemark());
            map.put("precautions",up.getPrecautions());
            list.add(map);
        }

        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    /*
    * 保存工作项
    * */
    @Override
    public DataResult insertUpkeepDbTask(LoginInfo li, Long treeId, Long emId, String taskName, String taskDesc, String remark,String precautions) throws Exception {
        /*if(StrUtil.isNotBlank(data)){
            for (Object obj : JSONUtil.parseArray(data)) {
                JSONObject jsonObj = JSONUtil.parseObj(obj);
                UpkeepDbTask upkeepDbTask = new UpkeepDbTask();
                upkeepDbTask.setEmId(emId);
                upkeepDbTask.setTreeId(treeId);
                upkeepDbTask.setTaskName(jsonObj.getStr("taskName",""));
                upkeepDbTask.setTaskDesc(jsonObj.getStr("taskDesc",""));
                upkeepDbTask.setRemark(jsonObj.getStr("remark",""));
                upkeepDbTask.setCreateTime(new Date());
                upkeepDbTaskMapper.insert(upkeepDbTask);
            }
        }*/
        UpkeepDbTask upkeepDbTask = new UpkeepDbTask();
        upkeepDbTask.setEmId(emId);
        upkeepDbTask.setTreeId(treeId);
        upkeepDbTask.setTaskName(taskName);
        upkeepDbTask.setTaskDesc(StrUtil.isBlank(taskDesc) ? "" : taskDesc);
        upkeepDbTask.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        upkeepDbTask.setPrecautions(StrUtil.isBlank(precautions) ? "" : precautions);
        upkeepDbTask.setCreateTime(new Date());
        upkeepDbTaskMapper.insert(upkeepDbTask);
        return DataResult.build9100();
    }

    @Override
    public DataResult importBasicUpkeepDbTask(LoginInfo li,List<UpkeepDbTaskExportVo> records) throws Exception {
        ArrayList<UpkeepDbTask> upkeepDbTasks = new ArrayList<>();
        //设备
        if(records.size()>0){
            for (UpkeepDbTaskExportVo record : records) {
                UpkeepDbTask upkeepDbTask = new UpkeepDbTask();
                upkeepDbTask.setEmId(equipmentInfoMapper.emId(record.getEmIdName()));
                upkeepDbTask.setTreeId(0l);
                upkeepDbTask.setTaskName(record.getTaskName());
                upkeepDbTask.setTaskDesc(record.getTaskDesc());
                upkeepDbTask.setPrecautions(record.getPrecautions());
                upkeepDbTask.setRemark(record.getRemark());
                upkeepDbTask.setCreateTime(new Date());
                upkeepDbTasks.add(upkeepDbTask);
            }
        }
        int rows = upkeepDbTaskMapper.batchInsert(upkeepDbTasks);
        return DataResult.build9100(rows);
    }

    @Override
    public DataResult updateUpkeepDbTask(LoginInfo li, Long id, Long treeId, Long emId, String taskName, String taskDesc, String remark,String precautions) throws Exception {
        UpkeepDbTask upkeepDbTask = new UpkeepDbTask();
        upkeepDbTask.setId(id);
        upkeepDbTask.setEmId(emId);
        upkeepDbTask.setTreeId(treeId);
        upkeepDbTask.setTaskDesc(StrUtil.isBlank(taskDesc) ? "" : taskDesc);
        upkeepDbTask.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        upkeepDbTask.setPrecautions(StrUtil.isBlank(precautions) ? "" : precautions);
        upkeepDbTaskMapper.updateByPrimaryKeySelective(upkeepDbTask);
        return DataResult.build9100();
    }

    @Override
    public DataResult deleteUpkeepDbTask(LoginInfo li, long id) throws Exception {
        upkeepDbTaskMapper.deleteByPrimaryKey(id);
        return DataResult.build9100();
    }

    @Override
    public DataResult getUpkeepCalendar(LoginInfo loginInfo, UpkeepCalendarDto upkeepCalendarDto) throws Exception {
        Page<EquipmentInfo> pageObject = PageHelper.startPage(Integer.parseInt(upkeepCalendarDto.getPageNum()), Integer.parseInt(upkeepCalendarDto.getPageSize()));
        Integer year = Integer.parseInt(upkeepCalendarDto.getYear());
        Long treeId = Long.parseLong(upkeepCalendarDto.getTreeId());
        Long emId = null;
        if (StrUtil.isNotBlank(upkeepCalendarDto.getEmId())) {
            emId = Long.parseLong(upkeepCalendarDto.getEmId());
        }
        List<PlanCalendarVo> groupVoList = upkeepDaoMapper.getUpkeepPlanGroup(year, treeId, emId);
        List<PlanCalendarVo> planCalendarVos = upkeepDaoMapper.getUpkeepPlanListByYear(year, treeId, emId);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> planRowList = getUpkeepPlanRowList(year, Integer.parseInt(upkeepCalendarDto.getType()), planCalendarVos);
        Map<Long, List<Map<String, Object>>> templateGroupMap = (Map<Long, List<Map<String, Object>>>) planRowList.get("TemplateGroupMap");
        for (PlanCalendarVo vo : groupVoList) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", vo.getTemplateName());
            map.put("id", vo.getTemplateId());
            map.put("list", templateGroupMap.get(vo.getTemplateId()));
            list.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("headDateList", planRowList.get("headData"));
        map.put("total", pageObject.getTotal());
        map.put("rows", list);
        return DataResult.build9100(map);
    }

    @Override
    public DataResult selectUpkeepCalendarEm(LoginInfo loginInfo, Long treeId, Integer year) throws Exception {
        List<Map<String,Object>> list = upkeepDaoMapper.selectUpkeepCalendarEm(treeId, year);
        return DataResult.build9100(list);
    }


    //type=1 日 type=2 周 type=3 年
    private Map<String, Object> getPlanRowList(int year, int type, List<PlanCalendarVo> planCalendarVos) {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> headDateMap = new HashMap<>();
        headDateMap.put("year", year);
        List<Map<String, Object>> monthList = new ArrayList<>();
        Map<Long, List<Map<String, Object>>> TemplateGroupMap = new HashMap<>();
        //第几周的标识
        int j = 0;
        Map<Long, List<PlanCalendarVo>> collect = null;
        //数据分组
        if (CollUtil.isNotEmpty(planCalendarVos)) {
            collect = planCalendarVos.stream().collect(Collectors.groupingBy(PlanCalendarVo::getEmId));
        }
        WeekMothVo weekMothVo = new WeekMothVo();
        for (Month month : Month.values()) {
            Map<String, Object> monthMap = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.set(year, month.ordinal(), 1);
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            List<Integer> dayList = new ArrayList<>();
            if (type == 1 || type == 2) {
                for (int i = 1; i <= Maximum; i++) {
                    cal.set(year, month.ordinal(), i);
                    //每周日
                    if (type == 1) {
                        //头部信息
                        dayList.add(i);
                        //详情信息
                        assemPlan(collect, month, i, j, TemplateGroupMap, type, null, Maximum);
                    } else {
                        Integer mothOne = weekMothVo.getMothOne();
                        if (mothOne == null) {
                            weekMothVo.setMothOne(month.getValue());
                            List<Integer> weekListNew = new ArrayList<>();
                            weekListNew.add(i);
                            weekMothVo.setMothOneList(weekListNew);
                        } else {
                            if (mothOne == month.getValue()) {
                                List<Integer> mothOneList = weekMothVo.getMothOneList();
                                mothOneList.add(i);
                                weekMothVo.setMothOneList(mothOneList);
                            } else {
                                Integer mothTwo = weekMothVo.getMothTwo();
                                if (mothTwo == null) {
                                    weekMothVo.setMothTwo(month.getValue());
                                    List<Integer> weekListNew = new ArrayList<>();
                                    weekListNew.add(i);
                                    weekMothVo.setMothTwoList(weekListNew);
                                } else {
                                    List<Integer> mothTwoList = weekMothVo.getMothTwoList();
                                    mothTwoList.add(i);
                                    weekMothVo.setMothTwoList(mothTwoList);
                                }
                            }
                        }
                        //头部信息
                        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                            j++;
                            dayList.add(j);
                            assemPlan(collect, month, i, j, TemplateGroupMap, type, weekMothVo, Maximum);
                            weekMothVo = new WeekMothVo();
                        }
                    }
                }
                monthMap.put("column", dayList.toArray());
            } else {
                assemPlan(collect, month, 0, j, TemplateGroupMap, type, null, Maximum);
                monthMap.put("column", month.getValue());
            }
            monthMap.put("monthName", month.getDisplayName(TextStyle.FULL, Locale.CHINESE));
            monthList.add(monthMap);
        }
        headDateMap.put("monthList", monthList);
        returnMap.put("headData", headDateMap);
        returnMap.put("TemplateGroupMap", TemplateGroupMap);
        return returnMap;
    }

    //type=1 日 type=2 周 type=3 年
    private Map<String, Object> getUpkeepPlanRowList(int year, int type, List<PlanCalendarVo> planCalendarVos) {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> headDateMap = new HashMap<>();
        headDateMap.put("year", year);
        List<Map<String, Object>> monthList = new ArrayList<>();
        Map<Long, List<Map<String, Object>>> TemplateGroupMap = new HashMap<>();
        //第几周的标识
        int j = 0;
        Map<Long, List<PlanCalendarVo>> collect = null;
        //数据分组
        if (CollUtil.isNotEmpty(planCalendarVos)) {
            collect = planCalendarVos.stream().collect(Collectors.groupingBy(PlanCalendarVo::getTemplateId));
        }
        WeekMothVo weekMothVo = new WeekMothVo();
        for (Month month : Month.values()) {
            Map<String, Object> monthMap = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.set(year, month.ordinal(), 1);
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            List<Integer> dayList = new ArrayList<>();
            if (type == 1 || type == 2) {
                for (int i = 1; i <= Maximum; i++) {
                    cal.set(year, month.ordinal(), i);
                    //每周日
                    if (type == 1) {
                        //头部信息
                        dayList.add(i);
                        //详情信息
                        assemPlan(collect, month, i, j, TemplateGroupMap, type, null, Maximum);
                    } else {
                        Integer mothOne = weekMothVo.getMothOne();
                        if (mothOne == null) {
                            weekMothVo.setMothOne(month.getValue());
                            List<Integer> weekListNew = new ArrayList<>();
                            weekListNew.add(i);
                            weekMothVo.setMothOneList(weekListNew);
                        } else {
                            if (mothOne == month.getValue()) {
                                List<Integer> mothOneList = weekMothVo.getMothOneList();
                                mothOneList.add(i);
                                weekMothVo.setMothOneList(mothOneList);
                            } else {
                                Integer mothTwo = weekMothVo.getMothTwo();
                                if (mothTwo == null) {
                                    weekMothVo.setMothTwo(month.getValue());
                                    List<Integer> weekListNew = new ArrayList<>();
                                    weekListNew.add(i);
                                    weekMothVo.setMothTwoList(weekListNew);
                                } else {
                                    List<Integer> mothTwoList = weekMothVo.getMothTwoList();
                                    mothTwoList.add(i);
                                    weekMothVo.setMothTwoList(mothTwoList);
                                }
                            }
                        }
                        //头部信息
                        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                            j++;
                            dayList.add(j);
                            assemPlan(collect, month, i, j, TemplateGroupMap, type, weekMothVo, Maximum);
                            weekMothVo = new WeekMothVo();
                        }
                    }
                }
                monthMap.put("column", dayList.toArray());
            } else {
                assemPlan(collect, month, 0, j, TemplateGroupMap, type, null, Maximum);
                monthMap.put("column", month.getValue());
            }
            monthMap.put("monthName", month.getDisplayName(TextStyle.FULL, Locale.CHINESE));
            monthList.add(monthMap);
        }
        headDateMap.put("monthList", monthList);
        returnMap.put("headData", headDateMap);
        returnMap.put("TemplateGroupMap", TemplateGroupMap);
        return returnMap;
    }

    //type=1 日 type=2 周 type=3 年
    private Map<String, Object> getPatrolPlanRowList(int year, int type, List<PatrolPlanCalendarVo> planCalendarVos) {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> headDateMap = new HashMap<>();
        headDateMap.put("year", year);
        List<Map<String, Object>> monthList = new ArrayList<>();
        Map<Long, List<Map<String, Object>>> TemplateGroupMap = new HashMap<>();
        //第几周的标识
        int j = 0;
        Map<Long, List<PatrolPlanCalendarVo>> collect = null;
        //数据分组
        if (CollUtil.isNotEmpty(planCalendarVos)) {
            collect = planCalendarVos.stream().collect(Collectors.groupingBy(PatrolPlanCalendarVo::getEmTree));
        }
        WeekMothVo weekMothVo = new WeekMothVo();
        for (Month month : Month.values()) {
            Map<String, Object> monthMap = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.set(year, month.ordinal(), 1);
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            List<Integer> dayList = new ArrayList<>();
            if (type == 1 || type == 2) {
                for (int i = 1; i <= Maximum; i++) {
                    cal.set(year, month.ordinal(), i);
                    //每周日
                    if (type == 1) {
                        //头部信息
                        dayList.add(i);
                        //详情信息
                        assemPatrolPlan(collect, month, i, j, TemplateGroupMap, type, null, Maximum);
                    } else {
                        Integer mothOne = weekMothVo.getMothOne();
                        if (mothOne == null) {
                            weekMothVo.setMothOne(month.getValue());
                            List<Integer> weekListNew = new ArrayList<>();
                            weekListNew.add(i);
                            weekMothVo.setMothOneList(weekListNew);
                        } else {
                            if (mothOne == month.getValue()) {
                                List<Integer> mothOneList = weekMothVo.getMothOneList();
                                mothOneList.add(i);
                                weekMothVo.setMothOneList(mothOneList);
                            } else {
                                Integer mothTwo = weekMothVo.getMothTwo();
                                if (mothTwo == null) {
                                    weekMothVo.setMothTwo(month.getValue());
                                    List<Integer> weekListNew = new ArrayList<>();
                                    weekListNew.add(i);
                                    weekMothVo.setMothTwoList(weekListNew);
                                } else {
                                    List<Integer> mothTwoList = weekMothVo.getMothTwoList();
                                    mothTwoList.add(i);
                                    weekMothVo.setMothTwoList(mothTwoList);
                                }
                            }
                        }
                        //头部信息
                        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                            j++;
                            dayList.add(j);
                            assemPatrolPlan(collect, month, i, j, TemplateGroupMap, type, weekMothVo, Maximum);
                            weekMothVo = new WeekMothVo();
                        }
                    }
                }
                monthMap.put("column", dayList.toArray());
            } else {
                assemPatrolPlan(collect, month, 0, j, TemplateGroupMap, type, null, Maximum);
                monthMap.put("column", month.getValue());
            }
            monthMap.put("monthName", month.getDisplayName(TextStyle.FULL, Locale.CHINESE));
            monthList.add(monthMap);
        }
        headDateMap.put("monthList", monthList);
        returnMap.put("headData", headDateMap);
        returnMap.put("TemplateGroupMap", TemplateGroupMap);
        return returnMap;
    }

    private String getBusinessTypeName(int businessType) {
        String businessTypeName = "";
        if (businessType == 1) {
            businessTypeName = "日保养";
        } else if (businessType == 2) {
            businessTypeName = "周保养";
        } else if (businessType == 3) {
            businessTypeName = "月保养";
        }
        return businessTypeName;
    }

    private String getPatrolBusinessTypeName(int businessType) {
        String businessTypeName = "";
        if (businessType == 1) {
            businessTypeName = "日巡检";
        } else if (businessType == 2) {
            businessTypeName = "周巡检";
        } else if (businessType == 3) {
            businessTypeName = "月巡检";
        } else if (businessType == 4) {
            businessTypeName = "例行巡检";
        }
        return businessTypeName;
    }

    /*
     * 组装数据
     * */
    private void assemPlan(Map<Long, List<PlanCalendarVo>> collect, Month month, int i, int j, Map<Long, List<Map<String, Object>>> TemplateGroupMap, int type, WeekMothVo weekMothVo, int Maximum) {
        if (CollUtil.isNotEmpty(collect)) {
            //循环设备
            for (Long key : collect.keySet()) {
                Map<String, Object> TemplateMap = new HashMap<>();
                List<Map<String, Object>> rowMapList = new ArrayList<>();
                List<PlanCalendarVo> collectGroup = collect.get(key);
                List<PlanCalendarVo> planCalendarVoGroupList = null;
                //获取每月每天所有的设备数据
                if (type == 1) {
                    planCalendarVoGroupList = collectGroup.stream().filter(t -> t.getMonth() == month.getValue() && t.getDay() == i).collect(Collectors.toList());
                } else if (type == 2) {
                    planCalendarVoGroupList = getPlanCalendarVoList(collectGroup, weekMothVo.getMothOne(), weekMothVo.getMothOneList());
                    if (weekMothVo.getMothTwo() != null) {
                        List<PlanCalendarVo> planCalendarVoList = getPlanCalendarVoList(collectGroup, weekMothVo.getMothTwo(), weekMothVo.getMothTwoList());
                        planCalendarVoGroupList.addAll(planCalendarVoList);
                    }
                } else if (type == 3) {
                    planCalendarVoGroupList = collectGroup.stream().filter(t -> t.getMonth() == month.getValue()).collect(Collectors.toList());
                }
                if (CollUtil.isNotEmpty(planCalendarVoGroupList)) {
                    //组装一天或一周或一月的模板数据
                    for (PlanCalendarVo planCalendarVo : planCalendarVoGroupList) {
                        Map<String, Object> rowMap = new HashMap<>();
                        rowMap.put("name", planCalendarVo.getTemplateName());
                        rowMap.put("changeTime", DateUtil.format(planCalendarVo.getChangeTime(), "yyyy-MM-dd"));
                        rowMap.put("businessTypeName", getBusinessTypeName(planCalendarVo.getBusinessType()));
                        rowMap.put("businessType", planCalendarVo.getBusinessType());
                        rowMapList.add(rowMap);
                    }
                    TemplateMap.put("template", rowMapList);
                    if (type == 1) {
                        TemplateMap.put("rangeTime", month.getValue() + "月" + i + "日");
                        TemplateMap.put("column", i);
                    } else if (type == 2) {
                        if (weekMothVo.getMothTwo() == null) {
                            TemplateMap.put("rangeTime", weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(0) + "日-" + weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(weekMothVo.getMothOneList().size() - 1) + "日");
                        } else {
                            TemplateMap.put("rangeTime", weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(0) + "日-" + weekMothVo.getMothTwo() + "月" + weekMothVo.getMothTwoList().get(weekMothVo.getMothTwoList().size() - 1) + "日");
                        }
                        TemplateMap.put("column", j);
                    } else if (type == 3) {
                        TemplateMap.put("rangeTime", month.getValue() + "月01日-" + month.getValue() + "月" + Maximum + "日");
                        TemplateMap.put("column", month.getValue());
                    }
                    TemplateMap.put("moth", month.getValue());
                    List<Map<String, Object>> mapList = TemplateGroupMap.get(key);
                    //所有日期下的数据组装成一个List,key为设备ID
                    if (CollUtil.isEmpty(mapList)) {
                        List<Map<String, Object>> newMapList = new ArrayList<>();
                        newMapList.add(TemplateMap);
                        TemplateGroupMap.put(key, newMapList);
                    } else {
                        mapList.add(TemplateMap);
                        TemplateGroupMap.put(key, mapList);
                    }
                }
            }
        }
    }

    /*
     * 组装数据
     * */
    private void assemPatrolPlan(Map<Long, List<PatrolPlanCalendarVo>> collect, Month month, int i, int j, Map<Long, List<Map<String, Object>>> TemplateGroupMap, int type, WeekMothVo weekMothVo, int Maximum) {
        if (CollUtil.isNotEmpty(collect)) {
            //循环设备
            for (Long key : collect.keySet()) {
                Map<String, Object> TemplateMap = new HashMap<>();
                List<Map<String, Object>> rowMapList = new ArrayList<>();
                List<PatrolPlanCalendarVo> collectGroup = collect.get(key);
                List<PatrolPlanCalendarVo> planCalendarVoGroupList = null;
                //获取每月每天所有的设备数据
                if (type == 1) {
                    planCalendarVoGroupList = collectGroup.stream().filter(t -> t.getMonth() == month.getValue() && t.getDay() == i).collect(Collectors.toList());
                } else if (type == 2) {
                    planCalendarVoGroupList = getPatrolPlanCalendarVoList(collectGroup, weekMothVo.getMothOne(), weekMothVo.getMothOneList());
                    if (weekMothVo.getMothTwo() != null) {
                        List<PatrolPlanCalendarVo> planCalendarVoList = getPatrolPlanCalendarVoList(collectGroup, weekMothVo.getMothTwo(), weekMothVo.getMothTwoList());
                        planCalendarVoGroupList.addAll(planCalendarVoList);
                    }
                } else if (type == 3) {
                    planCalendarVoGroupList = collectGroup.stream().filter(t -> t.getMonth() == month.getValue()).collect(Collectors.toList());
                }
                if (CollUtil.isNotEmpty(planCalendarVoGroupList)) {
                    //组装一天或一周或一月的模板数据
                    for (PatrolPlanCalendarVo planCalendarVo : planCalendarVoGroupList) {
                        Map<String, Object> rowMap = new HashMap<>();
                        rowMap.put("name", planCalendarVo.getPlanName());
                        if(planCalendarVo.getBusinessType() == 4){
                            rowMap.put("changeTime", DateUtil.format(planCalendarVo.getMinuteTime(), "yyyy-MM-dd HH:mm"));
                        }else {
                            rowMap.put("changeTime", DateUtil.format(planCalendarVo.getChangeTime(), "yyyy-MM-dd"));
                        }
                        rowMap.put("businessTypeName", getPatrolBusinessTypeName(planCalendarVo.getBusinessType()));
                        rowMap.put("businessType", planCalendarVo.getBusinessType());
                        rowMapList.add(rowMap);
                    }
                    TemplateMap.put("template", rowMapList);
                    if (type == 1) {
                        TemplateMap.put("rangeTime", month.getValue() + "月" + i + "日");
                        TemplateMap.put("column", i);
                    } else if (type == 2) {
                        if (weekMothVo.getMothTwo() == null) {
                            TemplateMap.put("rangeTime", weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(0) + "日-" + weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(weekMothVo.getMothOneList().size() - 1) + "日");
                        } else {
                            TemplateMap.put("rangeTime", weekMothVo.getMothOne() + "月" + weekMothVo.getMothOneList().get(0) + "日-" + weekMothVo.getMothTwo() + "月" + weekMothVo.getMothTwoList().get(weekMothVo.getMothTwoList().size() - 1) + "日");
                        }
                        TemplateMap.put("column", j);
                    } else if (type == 3) {
                        TemplateMap.put("rangeTime", month.getValue() + "月01日-" + month.getValue() + "月" + Maximum + "日");
                        TemplateMap.put("column", month.getValue());
                    }
                    TemplateMap.put("moth", month.getValue());
                    List<Map<String, Object>> mapList = TemplateGroupMap.get(key);
                    //所有日期下的数据组装成一个List,key为设备ID
                    if (CollUtil.isEmpty(mapList)) {
                        List<Map<String, Object>> newMapList = new ArrayList<>();
                        newMapList.add(TemplateMap);
                        TemplateGroupMap.put(key, newMapList);
                    } else {
                        mapList.add(TemplateMap);
                        TemplateGroupMap.put(key, mapList);
                    }
                }
            }
        }
    }

    private List<PlanCalendarVo> getPlanCalendarVoList(List<PlanCalendarVo> collectGroup, int moth, List<Integer> mothList) {
        return collectGroup.stream().filter(t -> t.getMonth() == moth && mothList.contains(t.getDay())).collect(Collectors.toList());
    }

    private List<PatrolPlanCalendarVo> getPatrolPlanCalendarVoList(List<PatrolPlanCalendarVo> collectGroup, int moth, List<Integer> mothList) {
        return collectGroup.stream().filter(t -> t.getMonth() == moth && mothList.contains(t.getDay())).collect(Collectors.toList());
    }

    //type=1 日 type=2 周 type=3 年
    private Map<String, Object> getHeadDateList(int year, int type) {
        Map<String, Object> headDateMap = new HashMap<>();
        headDateMap.put("year", year);
        List<Map<String, Object>> monthList = new ArrayList<>();
        //第几周的标识
        int j = 0;

        for (Month month : Month.values()) {
            Map<String, Object> monthMap = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.set(year, month.ordinal(), 1);
            int Maximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            List<Integer> dayList = new ArrayList<>();
            if (type == 1 || type == 2) {
                for (int i = 1; i <= Maximum; i++) {
                    cal.set(year, month.ordinal(), i);
                    //每周日
                    if (type == 1) {
                        dayList.add(i);
                    } else {
                        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                            j++;
                            dayList.add(j);
                        }
                    }
                }
            }
            monthMap.put("monthName", month.getDisplayName(TextStyle.FULL, Locale.CHINESE));
            monthMap.put("month", month.getValue());
            monthMap.put("day", dayList.toArray());
            monthList.add(monthMap);
        }
        headDateMap.put("monthList", monthList);
        return headDateMap;
    }

}
